package com.power.sifa.runner.jobs;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.plf.common.cache.redis.RedisCacheUtil;
import com.plf.common.utils.SpringHolder;
import com.power.sifa.base.model.Mq_logerr;
import com.power.sifa.base.service.Mq_logerrService;
import com.power.sifa.correction.model.Cre_record_ga;
import com.power.sifa.correction.model.enumeration.St_deal;
import com.power.sifa.correction.model.pageInfo.Cre_record_gaPageInfo;
import com.power.sifa.correction.service.Cre_record_gaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;

import java.util.Date;
import java.util.List;

/**
 * 微信模板消息异步发送线程
 *
 * @author 81814
 */
@Slf4j
public class WarningThread implements Runnable {
    /**
     * 队列名
     */
    public static final String MQNAME = "mq_group_warningthread";

    /**
     * 轮询周期毫秒
     */
    private static final long SLEEP_TIME = 300000L;

    private RedisCacheUtil redisCacheUtil;

    private Mq_logerrService mq_logerrService;

    private Cre_record_gaService cre_record_gaService;

    /**
     * 私有化构造，禁止外部创建
     */
    private WarningThread() {
    }

    /**
     * 单例对象
     */
    private static WarningThread self;

    /**
     * 单例获取对象
     *
     * @return
     */
    public static WarningThread getInstance() {
        if (self == null) {
            synchronized (WarningThread.class) {
                if (self == null) {
                    self = new WarningThread();
                }
            }
        }
        return self;
    }

    @Override
    public void run() {
        //线程类无法Autowire注入对象，全都要手工getBean初始化
        redisCacheUtil = SpringHolder.getBean(RedisCacheUtil.class);
        mq_logerrService = SpringHolder.getBean(Mq_logerrService.class);
        cre_record_gaService = SpringHolder.getBean(Cre_record_gaService.class);
        while (true) {
            log.info("WarningThread 正在轮询。。。");
            // 查询对应的案件内容
            Cre_record_gaPageInfo pageInfo = new Cre_record_gaPageInfo();
            pageInfo.setPageNumber(1);
            pageInfo.setPageSize(200);
            pageInfo.setF_status(St_deal.NO);
            Page<Cre_record_ga> record_gasPage = this.cre_record_gaService.findAll(pageInfo);
            if (ObjectUtil.isNotNull(record_gasPage) && CollUtil.isNotEmpty(record_gasPage.getContent())) {
                List<Cre_record_ga> record_gas = record_gasPage.getContent();
                try {
                    log.info("WarningThread 当前执行的数据大小{}。", record_gas.size());
                    this.cre_record_gaService.transRuleRequests(record_gas);
                    Mq_logerr err = new Mq_logerr();
                    err.setId(NanoIdUtils.randomNanoId());
                    err.setMqname(MQNAME);
                    err.setExectime(new Date());
                    err.setErrormsg("sucess");
                    mq_logerrService.save(err);
                    log.info("WarningThread 执行成功");
                } catch (Exception ex) {
                    //失败日志入库
                    log.error("WarningThread 执行异常:{}", ex.getMessage());
                    Mq_logerr err = new Mq_logerr();
                    err.setId(NanoIdUtils.randomNanoId());
                    err.setMqname(MQNAME);
                    err.setExectime(new Date());
                    err.setErrormsg(ex.getMessage());
                    mq_logerrService.save(err);
                }
            }
//            //查询队列内容
//            Long len = redisCacheUtil.llen(MQNAME);
//            log.info("WarningThread 队列长度：{}。", len);
//            if (len > 0) {
//                //读取全部队列数据
//                List list = redisCacheUtil.leftPops(MQNAME, len.intValue());
//                for (Object data : list) {
//                    try {
//                        //消费队列数据
//                        HashMap map = (HashMap) data;
//                        String taskId = (String) map.get("taskId");
//                        log.info("WarningThread 当前执行的队列taskId：{}。", taskId);
//                        this.cre_record_gaService.validateRecord(taskId);
//                        Mq_logerr err = new Mq_logerr();
//                        err.setId(NanoIdUtils.randomNanoId());
//                        err.setMqname(MQNAME);
//                        err.setMqdata(JSON.toJSONString(data));
//                        err.setExectime(new Date());
//                        err.setErrormsg("sucess");
//                        mq_logerrService.save(err);
//                        log.info("WarningThread 执行成功");
//                    } catch (Exception ex) {
//                        //失败日志入库
//                        log.error("WarningThread 执行异常:{}", ex.getMessage());
//                        Mq_logerr err = new Mq_logerr();
//                        err.setId(NanoIdUtils.randomNanoId());
//                        err.setMqname(MQNAME);
//                        err.setMqdata(JSON.toJSONString(data));
//                        err.setExectime(new Date());
//                        err.setErrormsg(ex.getMessage());
//                        mq_logerrService.save(err);
//                    }
//                }
//            }
            try {
                Thread.sleep(SLEEP_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
